<?php

/**
 * +---------------------------------------------------------------
 * | Author: 北京永青宇创科技有限公司
 * | Date: 2020/03/12
 * | 版本: 6.0
 * +---------------------------------------------------------------
 * | CopyRight: 北京永青宇创科技有限公司 All rights reserved.
 * +---------------------------------------------------------------
 * | 版权声明：云豹不是一个自由软件，是云豹官方推出的商业源码。
 * | 严禁在未经许可的情况下拷贝、复制、传播、使用云豹的任意代码
 * | 如有违反，请立即删除，否则您将面临承担相应，法律责任的风险。
 * | 如果需要取得官方授权，请联系官方 https://www.gepardshop.com
 * +---------------------------------------------------------------
 */

namespace app\ticket\controller;

class Ticket extends TicketHome {
	public function Index()
	{
		// 获取所有记录
		$map['status']     = 1;
		$map['custom_uid'] = $this->auth->id;

		$list = $this->ticketModel->where($map)->select();
		$list = listObject2array($list);
		foreach($list as &$item) {
			$custom_info          = $this->userModel->find($item['custom_uid']);
			$item['custom_name']  = $custom_info['username'];
			$item['custom_email'] = $custom_info['email'];

			$staff_info         = $this->userModel->find($item['staff_uid']);
			$item['staff_name'] = $staff_info['username'];
		}
		// 使用Builder快速建立列表页面
		//        var_dump($list);
		$this->assign('meta_title', '工单系统');

		//使用builder快速建立页面
		$builder = new ListBuilder();

		//处理标题和按钮
		$builder->title('工单列表' . '管理')
		        ->buttonModalPopup(url('ticket/Edit'));

		//处理字段
		$builder->keyText('id', '工单号');
		$builder->keyLink('title', '标题', 'Ticket/Ticket/Detail?id=###');
		$builder->keyText('custom_name', '客户名称');
		$builder->keyText('custom_email', '客户邮箱');

		$builder->keyText('create_time', '提交时间');

		$builder->keyText('staff_name', '分配给');

		//渲染数据处理分页
		$builder->keyDoActionModalPopup('Ticket/Edit?id=###', lang('_EDIT_'))
		        ->keyDoActionDelete('Ticket/Del?id=###', lang('_DELETE_'))
		        ->data($list)
		        ->setTemplate('listbuilder/ucenter');

		return $builder->display();

		return $this->fetch();
	}

	public function Detail($id = 0)
	{
		if(request()->isPost()) {
			$id        = input('id');
			$message   = input('message');
			$staff_uid = input('staff_uid');
			$due_time  = input('due_time');
			$status_id = input('status_id');

			//有处理反馈消息，新增一条动态
			if( ! empty($message)) {
				$insert['ticket_id'] = $id;
				$insert['uid']       = $this->auth->id;
				$insert['content']   = $message;
				$insert['status']    = 1;

				$this->ticketMessageModel->save($insert);
			}
		}

		$this->assignDetail($id);
		$this->assignStaff();
		$this->assignStatus();
		$this->assignMessage($id);

		return $this->fetch('ticket/detail');
	}

	public function Modify()
	{

	}
}